package com.maila.xiaoaoschool.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.fragment.FindFragment;
import com.maila.xiaoaoschool.fragment.LocationFragment;
import com.maila.xiaoaoschool.fragment.PersonFragment;
import com.maila.xiaoaoschool.util.StatusBarUtil;

public class MainActivity  extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BottomNavigationBar bottomNavigationBar;
    int lastSelectedPosition = 0;
    private String TAG = MainActivity.class.getSimpleName();
    private LocationFragment mLocationFragment;
    private FindFragment mFindFragment;
    private PersonFragment mFavoritesFragment;
    FragmentManager fm;
    FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.selector_home_page, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.selector_find_page, "发现"))
                .addItem(new BottomNavigationItem(R.drawable.selector_my_page, "我的"))
                .setActiveColor(R.color.orange)
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();
        fm= getFragmentManager();
        bottomNavigationBar.setTabSelectedListener(this);
        setDefaultFragment();
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {

        transaction = fm.beginTransaction();
        mLocationFragment = LocationFragment.newInstance("首页");
        transaction.replace(R.id.tb, mLocationFragment);
        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        Log.d(TAG, "onTabSelected() called with: " + "position = [" + position + "]");
        transaction = fm.beginTransaction();
        //开启事务
        switch (position) {
            case 0:
                if (mLocationFragment == null) {
                    mLocationFragment = LocationFragment.newInstance("首页");
                }
                StatusBarUtil.setColor(this, Color.parseColor("#f2673e"),0);
                transaction.replace(R.id.tb, mLocationFragment);
                break;
            case 1:
                if (mFindFragment == null) {
                    mFindFragment = FindFragment.newInstance("发现");
                }
                StatusBarUtil.setColor(this, Color.parseColor("#f2673e"),0);
                transaction.replace(R.id.tb, mFindFragment);
                break;
            case 2:
                if (mFavoritesFragment == null) {
                    mFavoritesFragment = PersonFragment.newInstance("我的");
                }
                StatusBarUtil.setColor(this, Color.parseColor("#666666"),0);
                transaction.replace(R.id.tb, mFavoritesFragment);
                break;
            default:
                break;
        }
        // 事务提交
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {
        Log.d(TAG, "onTabUnselected() called with: " + "position = [" + position + "]");
    }

    @Override
    public void onTabReselected(int position) {

    }
}